//
//  STOrderNewDetailViewController.swift
//  SeaTransportManage
//
//  Created by guopengwen on 2021/11/30.
//

import UIKit

typealias SuitcaseBtnAction = () -> Void

// 提空箱详情
class STOrderNewDetailViewController: GPBaseViewController {
    
    var orderId: Int = 0
    var status: Int = 0
    var orderType: String = ""
    
    var orderInfo: Dictionary<String, Any> = [:]
    let requestApi = GPRequestApi.init()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setTitle()
        addTopBGView()
        
        let h = 288 * (kScreenWidth - 32) / 343
        let bgView = UIView.init()
        bgView.backgroundColor = UIColor.white
        bgView.layer.cornerRadius = 12
        view.addSubview(bgView)
        bgView.snp.makeConstraints { (make) in
            make.top.equalTo(54)
            make.left.equalTo(16)
            make.right.equalTo(-16)
            make.height.equalTo(h)
        }
        
        let imgView: UIImageView = UIImageView.init(image: UIImage.init(named: "icon_on"))
        bgView.addSubview(imgView)
        imgView.snp.makeConstraints { (make) in
            make.top.equalTo(-38)
            make.centerX.equalTo(bgView)
            make.width.equalTo(88)
            make.height.equalTo(97)
        }
        
        bgView.addSubview(orderNo)
        orderNo.snp.makeConstraints { (make) in
            make.top.equalTo(imgView.snp.bottom).offset(18)
            make.left.equalTo(17)
            make.height.equalTo(22)
        }
        
//        bgView.addSubview(detailBtn)
//        detailBtn.snp.makeConstraints { (make) in
//            make.centerY.equalTo(orderNo)
//            make.right.equalTo(-24)
//            make.width.equalTo(86)
//            make.height.equalTo(36)
//        }
        
        let blue_h = 144 * (kScreenWidth - 32) / 311
        let blueView = UIView.init()
        blueView.backgroundColor = COLOR_HEX(hexString: "#F4F9FF", alpha: 1)
        blueView.layer.cornerRadius = 10
        bgView.addSubview(blueView)
        blueView.snp.makeConstraints { (make) in
            make.top.equalTo(orderNo.snp.bottom).offset(23)
            make.left.equalTo(16)
            make.right.equalTo(-16)
            make.height.equalTo(blue_h)
        }
        
        blueView.addSubview(adderssView)
        adderssView.snp.makeConstraints { (make) in
            make.top.equalTo(15)
            make.left.equalTo(16)
            make.right.equalTo(-16)
            make.height.equalTo(18)
        }
        
        blueView.addSubview(connectView)
        connectView.snp.makeConstraints { (make) in
            make.top.equalTo(adderssView.snp.bottom).offset(15)
            make.left.equalTo(16)
            make.right.equalTo(-16)
            make.height.equalTo(18)
        }
        
        blueView.addSubview(phoneView)
        phoneView.snp.makeConstraints { (make) in
            make.top.equalTo(connectView.snp.bottom).offset(15)
            make.left.equalTo(16)
            make.right.equalTo(-16)
            make.height.equalTo(18)
        }
        
        blueView.addSubview(timeView)
        timeView.snp.makeConstraints { (make) in
            make.top.equalTo(phoneView.snp.bottom).offset(15)
            make.left.equalTo(16)
            make.right.equalTo(-16)
            make.height.equalTo(18)
        }
        
        let btn1 = UIButton.init(type: .custom)
        btn1.layer.cornerRadius = 25
        btn1.setTitleColor(COLOR_HEX(hexString: "#8F92A1", alpha: 1), for: UIControl.State.normal)
        btn1.setAttributedTitle(NSAttributedString.init(string:"流程指引", attributes: [NSAttributedString.Key.font : UIFont.systemFont(ofSize: 18, weight: UIFont.Weight.heavy)]), for: UIControl.State.normal)
        view.addSubview(btn1)
        btn1.snp.makeConstraints { (make) in
            make.centerX.equalTo(bgView)
            make.width.equalTo(74)
            make.height.equalTo(25)
            make.bottom.equalTo(-109)
        }
        
        if (status != 12) {
            view.addSubview(nextBtn)
            nextBtn.snp.makeConstraints { (make) in
                make.left.equalTo(56)
                make.right.equalTo(-56)
                make.height.equalTo(54)
                make.bottom.equalTo(btn1.snp.top).offset(-10)
            }
            nextBtn.addTarget(self, action: #selector(enterSuitcasePicturePage), for: UIControl.Event.touchUpInside)
        }
    
        btn1.addTarget(self, action: #selector(backGuidanceProcessPage), for: UIControl.Event.touchUpInside)
        
        requestWaitOrderList()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        requestWaitOrderList()
    }
    
    func setTitle() {
        if (orderType.equalTo("0")){
            if(status == 3) {
                setNavBarTitle("提空箱详情")
                nextBtn.setTitle("上传提空箱照片", for: .normal)
                timeView.noteLB.text = "提空箱详情"
            } else if (status == 6) {
                setNavBarTitle("装箱详情")
                nextBtn.setTitle("上传装箱照片", for: .normal)
                timeView.noteLB.text = "装箱详情"
            }  else if (status == 9) {
                setNavBarTitle("落重详情")
                nextBtn.setTitle("上传落重箱照片", for: .normal)
                timeView.noteLB.text = "落重详情"
            }
        } else {
            if(status == 3) {
                setNavBarTitle("提重详情")
                nextBtn.setTitle("上传提重照片", for: .normal)
                timeView.noteLB.text = "提重详情"
            }  else if (status == 6) {
                setNavBarTitle("卸货详情")
                nextBtn.setTitle("上传卸货照片", for: .normal)
                timeView.noteLB.text = "卸货详情"
            }  else if (status == 9) {
                setNavBarTitle("还空箱详情")
                nextBtn.setTitle("上传还空箱照片", for: .normal)
                timeView.noteLB.text = "还空箱详情"
            }
        }
    }
    
    func requestWaitOrderList() {
        requestApi.getOrderDetail(["orderId": orderId]) {
            (isSuccess, response, error)  in
            self.hideLoading()
            if(isSuccess) {
                let responseData = response as! Dictionary<String, Any>
                let data = responseData["data"] as! Dictionary<String, Any>
                self.orderInfo = data
                self.orderNo.text = "订单：".appending("20200311011588")
                self.adderssView.update(data["pickUpFrom"] as! String)
                self.connectView.update(data["pickUpContact"] as! String)
                self.phoneView.update(data["pickUpPhone"] as! String)
                self.timeView.update(data["pickUpTime"] as! String)
                self.status = data["status"] as! Int
                self.orderType = data["orderType"] as! String
                self.setTitle()
            }
        }
    }
    
    @objc func enterSuitcasePicturePage() {
        if (orderType.equalTo("0") ) {
            if(status == 3) {
                let vc: STSuitcasePictureViewController = STSuitcasePictureViewController.init()
                vc.orderId = orderId
                navigationController?.pushViewController(vc, animated: true)
            } else if (status == 6) {
                let vc: STEnchasePictureViewController = STEnchasePictureViewController.init()
                vc.orderId = orderId
                navigationController?.pushViewController(vc, animated: true)
            } else if (status == 9) {
                let vc: STDropPictureViewController = STDropPictureViewController.init()
                vc.orderId = orderId
                navigationController?.pushViewController(vc, animated: true)
            }
        } else {
            if(status == 3) {
                let vc: STLiftPictureViewController = STLiftPictureViewController.init()
                vc.orderId = orderId
                navigationController?.pushViewController(vc, animated: true)
            } else if (status == 6) {
                let vc: STUnloadPictureViewController = STUnloadPictureViewController.init()
                vc.orderId = orderId
                navigationController?.pushViewController(vc, animated: true)
            } else if (status == 9) {
                let vc: STRepayPictureViewController = STRepayPictureViewController.init()
                vc.orderId = orderId
                navigationController?.pushViewController(vc, animated: true)
            }
        }
    }
   
    // 流程指引
    @objc func backGuidanceProcessPage() {
        let vc: STGuidanceViewController = STGuidanceViewController.init()
        navigationController?.pushViewController(vc, animated: true)
    }
    
    func enterSuitcaseDetail() {
        let vc = STOrderNewDetailFileViewController.init()
        navigationController?.pushViewController(vc, animated: true)
    }
    
    lazy var orderNo: UILabel = {
        let lb = UILabel.init()
        lb.textAlignment = .center
        lb.textColor = UIColor.init(hexString: "#323232", alpha: 1)
        lb.font = UIFont.systemFont(ofSize: 16, weight: UIFont.Weight.heavy)
        return lb
    }()
    
//    lazy var detailBtn: STOrderSuitcaseDetailView = {
//        let btn = STOrderSuitcaseDetailView.init(frame: CGRect.init(x: 0, y: 0, width: 86, height: 36)) {
//            self.enterSuitcaseDetail()
//        }
//        return btn
//    }()
//
    lazy var adderssView: STOrderSuitcaseInfoView = {
        let v: STOrderSuitcaseInfoView = STOrderSuitcaseInfoView.init(frame: CGRect.zero, type: 0)
        return v
    }()
    
    lazy var connectView: STOrderSuitcaseInfoView = {
        let v: STOrderSuitcaseInfoView = STOrderSuitcaseInfoView.init(frame: CGRect.zero, type: 1)
        return v
    }()
    
    
    lazy var phoneView: STOrderSuitcaseInfoView = {
        let v: STOrderSuitcaseInfoView = STOrderSuitcaseInfoView.init(frame: CGRect.zero, type: 2)
        return v
    }()
    
    lazy var timeView: STOrderSuitcaseInfoView = {
        let v: STOrderSuitcaseInfoView = STOrderSuitcaseInfoView.init(frame: CGRect.zero, type: 3)
        return v
    }()
    
    lazy var nextBtn: UIButton = {
        let btn = UIButton.init(type: .custom)
        btn.layer.cornerRadius = 27
        btn.backgroundColor = COLOR_HEX(hexString: "#3686FF", alpha: 1)
        btn.setTitleColor(UIColor.white, for: UIControl.State.normal)
        return btn
    }()
    
}

class STOrderSuitcaseDetailView: UIView {
    
    var suitcaseBtnAction: SuitcaseBtnAction?
    
    convenience init(frame: CGRect, forSuitcaseDetail action: @escaping SuitcaseBtnAction) {
        self.init(frame: frame)
        suitcaseBtnAction = action
        layer.cornerRadius = 18
        backgroundColor = COLOR_HEX(hexString: "#008CFF", alpha: 0.2)
        
        let lb = UILabel.init()
        lb.textAlignment = .left
        lb.text = "详情"
        lb.textColor = UIColor.init(hexString: "#008CFF", alpha: 1)
        lb.font = UIFont.systemFont(ofSize: 20, weight: UIFont.Weight.heavy)
        addSubview(lb)
        lb.snp.makeConstraints { (make) in
            make.left.equalTo(18)
            make.centerY.equalTo(self)
            make.height.equalTo(22)
        }
        
        let imgView: UIImageView = UIImageView.init(image: UIImage.init(named: "arrow-next-blue"))
        addSubview(imgView)
        imgView.snp.makeConstraints { (make) in
            make.centerY.equalTo(lb)
            make.right.equalTo(-8)
            make.width.equalTo(20)
            make.height.equalTo(20)
        }
        
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        suitcaseBtnAction!()
    }
    
    override init(frame: CGRect) {
        self.suitcaseBtnAction = nil
        super.init(frame: frame)
    }
    
    required init?(coder: NSCoder) {
        self.suitcaseBtnAction = nil
        super.init(coder: coder)
        fatalError("init(coder:) has not been implemented")
    }
}

class STOrderSuitcaseInfoView: UIView {
    
    convenience init(frame: CGRect, type: Int) {
        self.init(frame: frame)
        
        let imgName = getImgName(type: type)
        let imgView: UIImageView = UIImageView.init(image: UIImage.init(named: imgName))
        addSubview(imgView)
        imgView.snp.makeConstraints { (make) in
            make.top.equalTo(0)
            make.left.equalTo(16)
            make.width.equalTo(18)
            make.height.equalTo(18)
        }
    
        addSubview(noteLB)
        noteLB.snp.makeConstraints { (make) in
            make.left.equalTo(imgView.snp.right).offset(8)
            make.centerY.equalTo(imgView)
            make.height.equalTo(21)
        }
        
        addSubview(descLB)
        descLB.snp.makeConstraints { (make) in
            make.left.equalTo(noteLB.snp.right).offset(0)
            make.centerY.equalTo(noteLB)
            make.height.equalTo(21)
        }
        
        noteLB.text = getDesc(type: type)
    }
    
    func update(_ value: String) {
        descLB.text = value
    }
    
    func getImgName(type: Int) -> String {
        switch type {
        case 0:
            return "icon_tixiang_dizhi"
        case 1:
            return "icon_lianxiren"
        case 2:
            return "icon_lianxifangshi"
        case 3:
            return "icon_shijian"
        default:
            return ""
        }
    }
    
    func getDesc(type: Int) -> String {
        switch type {
        case 0:
            return "地址："
        case 1:
            return "联系人："
        case 2:
            return "联系方式："
        case 3:
            return "提空箱时间："
        default:
            return ""
        }
    }
    
    lazy var noteLB: UILabel = {
        let lb = UILabel.init()
        lb.textAlignment = .left
        lb.textColor = UIColor.init(hexString: "#323232", alpha: 1)
        lb.font = UIFont.systemFont(ofSize: 15, weight: UIFont.Weight.heavy)
        return lb
    }()
    
    lazy var descLB: UILabel = {
        let lb = UILabel.init()
        lb.textAlignment = .left
        lb.textColor = UIColor.init(hexString: "#323232", alpha: 1)
        lb.font = UIFont.systemFont(ofSize: 15, weight: UIFont.Weight.heavy)
        return lb
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
